package com.leyou.search.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping("goods")
public class SearchController {
    @Autowired
    SearchService searchService;
    /**
     * 初始化索引库
     */
    @GetMapping("init")
    public ResponseEntity<String> init(){
        searchService.createIndexAndMapping();;
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }
    //- 请求方式：GET
    //- 请求路径：/goods/suggestion
    //- 请求参数：key，就是用户输入的关键字
    //返回值：提示给用户看的词条列表，应该是一个字符串的集合，需要Mono封装
    @GetMapping("suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key")String key){
        return  searchService.getSuggestion(key);
    }
    //查询业务
    @PostMapping("list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO request){
        return searchService.searchGoods(request);
    }
}
